using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace Acms.AcmsControls
{
    public class TemplatePage : Page
    {
        protected override void OnPreInit(EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["AcmsPostId"]))
            {
                try
                {
                    postId = int.Parse(Request.QueryString["AcmsPostId"]);
                }
                catch
                { }
            }

            if (!string.IsNullOrEmpty(Request.QueryString["AcmsSiteId"]))
            {
                try
                {
                    siteId = int.Parse(Request.QueryString["AcmsSiteId"]);
                }
                catch
                { }
            }

            base.OnPreInit(e);
        }

        protected override void OnPreLoad(EventArgs e)
        {
            base.OnPreLoad(e);
            if (!Page.IsPostBack)
            {
                if (CurrentPost != null)
                {
                    Page.Title = string.Format("{0} {1}", CurrentPost.SiteName, CurrentPost.DisplayName);
                    currentUrl = CurrentPost.Url;
                    currentPostDisplayName = CurrentPost.DisplayName;
                    currentPostName = CurrentPost.Name;
                }
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
         }
        protected override void OnLoadComplete(EventArgs e)
        {
            if (!string.IsNullOrEmpty(CurrentUrl))
            {
                StringBuilder tmpQS = new StringBuilder();
                string spacer = "?";
                foreach (string key in Request.QueryString.AllKeys)
                {
                    if (!key.StartsWith("Acms"))
                    {
                        tmpQS.Append(spacer);
                        tmpQS.Append(key);
                        tmpQS.Append("=");
                        tmpQS.Append(Request.QueryString[key]);
                        spacer = "&";

                    }
                }
                if (spacer == "?")
                {
                    tmpQS.Append(spacer);
                }
                Context.RewritePath(string.Concat(CurrentUrl, tmpQS.ToString()));
            }
            base.OnLoadComplete(e);
        }

        private int postId;
        public int PostId
        {
            get { return postId; }
        }

        private int siteId;
        public int SiteId
        {
            get { return siteId; }
        }


        private Acms.Bll.Post currentPost;
        public Acms.Bll.Post CurrentPost
        {
            get 
            {
                if (currentPost == null && postId > 0)
                    currentPost = Acms.Bll.Post.GetById(postId);
                return currentPost; 
            }
        }

        private string currentUrl;
        public string CurrentUrl
        {
            get { return currentUrl; }
        }

        private string currentPostDisplayName;
        public string CurrentPostDisplayName
        {
            get { return currentPostDisplayName; }
        }

        private string currentPostName;
        public string CurrentPostName
        {
            get { return currentPostName; }
        }

	
        protected override System.Web.UI.PageStatePersister PageStatePersister
        {
            get
            {
                return this.AcmsPageStatePersister;
            }
        }

        private Acms.StateManager.WebPageStatePersister psgPageStatePersister;
        /// <summary>
        /// Property PsgPageStatePersister
        /// </summary>
        public Acms.StateManager.WebPageStatePersister AcmsPageStatePersister
        {
            get
            {
                if (psgPageStatePersister == null)
                {
                    psgPageStatePersister = new Acms.StateManager.WebPageStatePersister(this);
                }
                return psgPageStatePersister;
            }
        }

        protected override System.Collections.Specialized.NameValueCollection DeterminePostBackMode()
        {
            return AcmsPageStatePersister.DeterminePostBackMode();
        }

        protected override object SaveViewState()
        {
            ViewState["Title"] = this.Title;
            ViewState["CurrentUrl"] = this.currentUrl;
            ViewState["CurrentPostDisplayName"] = currentPostDisplayName;
            ViewState["CurrentPostName"] = currentPostName;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            this.Title = ViewState["Title"] as string;
            this.currentUrl = ViewState["CurrentUrl"] as string;
            currentPostDisplayName = ViewState["CurrentPostDisplayName"] as string;
            currentPostName = ViewState["CurrentPostName"] as string;
        }

        public new Acms.Bll.UserPrinciple User
        {
            get
            {
                return base.User as Acms.Bll.UserPrinciple;
            }

        }

        public string GetLoginUrl()
        {
            return string.Concat(System.Web.Security.FormsAuthentication.LoginUrl, System.Web.Security.FormsAuthentication.LoginUrl.Contains("?") ? "&" : "?", "ReturnUrl=", CurrentUrl);
        }

        public string       GetSiteRoot()
        {
            System.Text.StringBuilder siteRoot = new System.Text.StringBuilder();
            if(Request.IsSecureConnection)
                siteRoot.Append("https://");
            else
                siteRoot.Append("http://");
    
            siteRoot.Append(Request.ServerVariables["Server_Name"]);
            
            if (Request.Url.Port != 80)
            {
                siteRoot.Append(":");
                siteRoot.Append(Request.Url.Port);
            }
            siteRoot.Append(Request.ApplicationPath);

            if (!siteRoot.ToString().EndsWith("/"))
                siteRoot.Append("/");

            string tmp = siteRoot.ToString();
            return tmp.Substring(0, tmp.Length - 1);

        }
    }
}
